import { genderStatus, eduStatus } from '@/config/defaultStatus/initStatus';
import {
  ComsTypeEnum,
  hasOptions,
  isPicTitleDescStatusArray,
  isStringArray,
  isValueStatusArray,
  ValueIndex,
  type Material,
  type OptionsProps,
  type PicLink,
  type Status,
  type TextProps
  // type TypeStatus
} from '@/types';

// const FieldName = 'status'
export function setTextStatus(textProps: TextProps, text: string) {
  console.log('设置文本的值', textProps, text);
  textProps.status = text;
}
// 设置当前选择项
export function setCurrentStatus(optionProps: OptionsProps, index: number) {
  optionProps.currentStatus = index;
}
// 设置指定项内容：如optionProps.status为[1,2]传入payload={value:2,index:0},则改为[2,2]
// {value:2,index:0，key:'status'}
export function setValueByIndex(optionProps: OptionsProps, payload: ValueIndex) {
  console.log('根据下标设置数组指定项目的值', optionProps, payload);
  if (isStringArray(optionProps.status)) {
    // [1,2]=>[2,2]
    optionProps.status[payload.index] = (payload as ValueIndex).value;
  } else if (isValueStatusArray(optionProps.status) || isPicTitleDescStatusArray(optionProps.status)) {
    // [{value: ''，picTitle:'图片标题'，picDesc:'图片描述' }]设置为
    // [{value: '图片链接'，picTitle:'图片标题'，picDesc:'图片描述' }]
    const key = payload.key || 'value';
    optionProps.status[payload.index][key] = payload.value;
  }
}
export function setPicLinkByIndex(optionProps: OptionsProps, payload: PicLink) {
  // console.log(optionProps, index)
  if (isPicTitleDescStatusArray(optionProps.status)) {
    optionProps.status[payload.index].value = payload.link;
  }
}
// 添加选项
export function addOption(optionProps: OptionsProps) {
  if (isStringArray(optionProps.status)) {
    let addIndex = 1;
    if (optionProps.status.length) {
      const endItem = optionProps.status[optionProps.status.length - 1];
      const endNum = Number(endItem[endItem.length - 1]);
      addIndex = isNaN(endNum) ? 1 : endNum + 1;
    }
    optionProps.status.push(`新选项${addIndex}`);
  } else if (isPicTitleDescStatusArray(optionProps.status)) {
    optionProps.status.push({
      value: '',
      picTitle: '',
      picDesc: ''
    });
  } else if (isValueStatusArray(optionProps.status)) {
    optionProps.status.push({
      value: '',
      status: ''
    });
  }
}

export function rmOption(optionProps: OptionsProps, index: number) {
  if (optionProps.status.length === 2) {
    return false;
  }
  optionProps.status.splice(index, 1);
  return true;
}

// 初始化预设组件store
export function updateInitStatusBeforeAdd(comStatus: Status, newMaterialName: Material) {
  if (newMaterialName === ComsTypeEnum.personInfoGender) {
    comStatus.name = ComsTypeEnum.personInfoGender;
    comStatus.status.title.status = '您的性别是？';
    if (hasOptions(comStatus.status)) comStatus.status.options.status = genderStatus();
  } else if (newMaterialName === ComsTypeEnum.personInfoEdu) {
    comStatus.name = ComsTypeEnum.personInfoEdu;
    comStatus.status.title.status = '您的学历是？';
    if (hasOptions(comStatus.status)) comStatus.status.options.status = eduStatus();
  }
}
